home *** CD-ROM | disk | FTP | other *** search
/ Clickx 65 / Clickx 65.iso / software / internet / xmarks / xmarks-3.1.1.xpi / chrome / content / foxmarks-progress.js < prev    next >
Encoding:
JavaScript  |  2009-05-05  |  5.4 KB  |  179 lines

  1. /* 
  2.  Copyright 2005-2008 Foxmarks Inc.
  3.  
  4.  foxmarks-progress.js: handles the UI and top-level logic for
  5.  merge, sync, upload, and download operations.
  6.   
  7.  */
  8.  
  9. var gCancelled;
  10. var gQuick;
  11. var gSyncTypes = ["bookmarks", "passwords"];
  12.  
  13. var newObserver = {
  14.     _document: null,
  15.     _window: null,
  16.  
  17.     observe: function(subject, topic, data) {
  18.         var result = eval(data);
  19.         //LogWrite("Progress: " + data);
  20.         if (this._document) {
  21.             if(result.status == 3){
  22.                 var label = this._document.getElementById(result.component);
  23.                 if(label){
  24.                     label.setAttribute("value",  result.phase == "end" ?
  25.                         Bundle().GetStringFromName("progress.sync.done") :
  26.                         Bundle().GetStringFromName("progress.sync.working")
  27.                     );
  28.                 }
  29.                 var image =this._document.getElementById(result.component + "-check");
  30.                 if(image){
  31.                     image.setAttribute("src", result.phase == "end" ?
  32.                         "chrome://foxmarks/skin/images/progress-good.png":
  33.                         "chrome://foxmarks/skin/images/wheel.gif"
  34.                     );
  35.                 }
  36.             }
  37.             else if (result.status != 1) { // complete?
  38.                 this._window.arguments[1].status = result.status;
  39.                 this._window.arguments[1].msg = result.msg;
  40.                 this._window.arguments[1].result = result;
  41.                 if (gQuick) {
  42.                     setTimeout(this._window.close, 1000);
  43.                 } else {
  44.                     gCancelled = true;
  45.                     setTimeout(this._window.close, 100);
  46.                 }
  47.             }
  48.         }        
  49.         try {
  50.             this._window.sizeToContent();
  51.         } catch(e) {}
  52.     },
  53.  
  54.     adjustDialogButtons: function(status) {
  55.         var d = this._document.documentElement;
  56.         var help = d.getButton("help");
  57.         help.hidden = (status >= 0 && status <= 2);
  58.         var cancel = d.getButton("cancel");
  59.         cancel.hidden = (status != 1);
  60.         try {
  61.             this._window.sizeToContent();
  62.         } catch(e) {}
  63.     }
  64. };
  65.  
  66. var gFoxmarksService = null;
  67. var Cc = Components.classes;
  68. var Ci = Components.interfaces;
  69.  
  70. function onProgressLoad() {
  71.     gCancelled = false;
  72.     gQuick = false;
  73.  
  74.     // get reference to foxmarks-service to process the request
  75.     gFoxmarksService = Cc["@foxcloud.com/extensions/foxmarks;1"].
  76.         getService(Ci.nsIFoxmarksService);
  77.  
  78.     var os = Cc["@mozilla.org/observer-service;1"].
  79.         getService(Ci.nsIObserverService);
  80.     newObserver._document = document;
  81.     newObserver._window = window;
  82.     os.addObserver(newObserver, "foxmarks-service", false);
  83.     
  84.     var okay = false;
  85.  
  86.     switch (window.arguments[0]) {
  87.         case "upload":
  88.             gQuick = false;
  89.             okay = gFoxmarksService.upload();
  90.             break;
  91.         
  92.         case "download":
  93.             gQuick = false;
  94.             okay = gFoxmarksService.download();
  95.             break;
  96.         
  97.         case "deletepasswords":
  98.             gQuick = false;
  99.             document.getElementById("lineitems").setAttribute("hidden", "true");
  100.             okay = gFoxmarksService.purgepasswords();
  101.             break;
  102.         case "synch":
  103.             gQuick = false;
  104.             okay = gFoxmarksService.synchronize();
  105.             break;
  106.  
  107.         case "status":
  108.             gQuick = true;
  109.             okay = gFoxmarksService.status();
  110.             break;
  111.             
  112.         case "initialSync":
  113.             var ca = window.arguments[2];
  114.             gQuick = true;
  115.             okay = gFoxmarksService.synchronizeInitial(ca.remoteIsMaster, 
  116.                 ca.merge);
  117.             break;
  118.     }
  119.  
  120.     var len = gSyncTypes.length;
  121.     var x;
  122.     for(x = 0; x < len; x++){
  123.         var type = gSyncTypes[x];
  124.         var label = document.getElementById(type);
  125.         label.setAttribute("value", gSettings.isSyncEnabled(type) ?
  126.             Bundle().GetStringFromName("progress.sync.enabled") :
  127.             Bundle().GetStringFromName("progress.sync.disabled")
  128.         );
  129.     }
  130.  
  131.     if (!okay) {
  132.         // Service is busy. Disconnect the observer and simulate a
  133.         // "busy" status message.
  134.         onProgressUnload();
  135.         newObserver.observe(null, null,
  136.             { msg: Bundle().GetStringFromName("msg.busy"), status: 4 }.
  137.             toSource());
  138.     }
  139. }
  140.  
  141. function onProgressUnload() {
  142.     var os = Cc["@mozilla.org/observer-service;1"].
  143.         getService(Ci.nsIObserverService);
  144.     try {
  145.         os.removeObserver(newObserver, "foxmarks-service");
  146.     } catch (e) {
  147.         LogWrite("Warning: removeObserver failed.");
  148.     }
  149. }
  150.  
  151. function onProgressCancel() {
  152.     gFoxmarksService.cancel();
  153.   
  154.     // the first time through, just let callbacks from the cancellation
  155.     // requests above shut us down
  156.     // if we somehow got stuck, allow a second press of the cancel
  157.     // button to actually shut the dialog box down.
  158.     
  159.     if (gCancelled) {
  160.         window.arguments[1].status = -1;
  161.         window.arguments[1].msg = Bundle().GetStringFromName("msg.cancelled");
  162.         return true;
  163.     } else {
  164.         gCancelled = true;
  165.         return false;   // let the callbacks close us down
  166.     }
  167. }
  168.  
  169. function onProgressHelp() {
  170.     var element = document.getElementById("status");
  171.     if (element) {
  172.         var errmsg = element.value.replace(/ /g, "_");
  173.         window.arguments[1].helpurl =
  174.             Bundle().formatStringFromName("url.error", [errmsg], 1);
  175.         setTimeout(window.close, 100);
  176.     }
  177.     return false;
  178. }
  179.